package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.ControlType;
import com.tbit.main.dao.core.MachineOccupyDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineOccupy;
import com.tbit.main.pojo.VO.MachineUnOccupyReqVO;
import com.tbit.main.pojo.bo.CommonControlParamBO;
import com.tbit.main.pojo.bo.TerControlBO;
import com.tbit.main.service.*;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service("machineOccupyService")
public class MachineOccupyServiceImpl implements MachineOccupyService {
    @Autowired
    private MachineOccupyDao machineOccupyDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MoveLogService moveLogService;
    @Autowired
    private DispatchLogService dispatchLogService;
    @Autowired
    private TerControlService terControlService;

    @Override
    public MachineOccupy getByMachineId(Integer machineId) {
        return machineOccupyDao.getByMachineId(machineId);
    }

    @Override
    public List<MachineOccupy> getByAccountId(Integer accountId) {
        return machineOccupyDao.getByAccountId(accountId);
    }

    @Override
    public List<MachineOccupy> getByRegionId(List<Integer> regionId) {
        return machineOccupyDao.getByRegionId(StringUtil.getListString(regionId));
    }

    @Override
    public List<MachineOccupy> getAll() {
        return machineOccupyDao.getAll();
    }

    @Override
    public void del(Integer machineId) {
        machineOccupyDao.del(machineId);
    }

    @Override
    public void insert(MachineOccupy machineOccupy) {
        machineOccupyDao.insert(machineOccupy);
    }

    @Override
    public void delActive() {
        machineOccupyDao.delActive();
    }

    @Override
    public Integer getByAccountUserId(Integer accountUserId) {
        return machineOccupyDao.getByAccountUserId(accountUserId);
    }

    @Override
    public void delBorrow() {
        machineOccupyDao.delBorrow();
    }

    @Override
    public List<Integer> getExist(List<Integer> list) {
        return machineOccupyDao.getExist(list);
    }

    @Override
    public List<MachineOccupy> getAccountUserId(List<Integer> machineIds) {
        return machineOccupyDao.getAccountUserId(machineIds);
    }

    @Override
    public List<MachineOccupy> getByMachineIds(List<Integer> machineIds) {
        return CollUtil.isEmpty(machineIds)
                ? Collections.emptyList()
                : machineOccupyDao.getByMachineIds(StringUtil.getListString(machineIds));
    }

    @Override
    public void unOccupy(MachineUnOccupyReqVO reqVO) {
        Machine machine = machineService.getByUseCode(reqVO.getUserCode());
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }
        machineService.auth(machine);
        machineOccupyDao.del(machine.getMachineId());
        moveLogService.delCache(machine.getUserCode());
        dispatchLogService.delCache(machine.getUserCode());

        String serNo = UUID.randomUUID().toString();
        terControlService.commonControl(new CommonControlParamBO(serNo, machine, ControlType.MACHINE_CONTROL_LOCK, "解除占用"));
    }

    @Override
    public void batchUnOccupy(List<Integer> machineIds) {
        List<Machine> machines = machineService.getByMachineIdsAll(machineIds);
        if (CollUtil.isEmpty(machines)) {
            throw new BaseException("Machine.notExist");
        }
        List<String> userCodes = machines.stream().map(Machine::getUserCode).collect(Collectors.toList());

        machineOccupyDao.batchDel(machineIds);
        moveLogService.batchDelCache(userCodes);
        dispatchLogService.batchDelCache(userCodes);

        terControlService.batchControl(machines, new TerControlBO(ControlType.MACHINE_CONTROL_LOCK, "批量划拨强制解除占用"));
    }

}